import axios from "axios";
// import store from "../store";
// import qs from "qs";
import {Message, MessageBox} from "element-ui";
// import { getToken, getMenuId } from "@/utils/auth";
const SITE = "";
// axios 配置
axios.defaults.timeout = 1500000000000;
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
// axios.defaults.baseURL = process.env.BASE_API

// POST传参序列化 拦截
axios.interceptors.request.use(
  (config) => {
    // if (store.getters.token) {
    //   config.headers["token"] = getToken(); // 让每个请求携带自定义token
    //   config.headers["menuId"] = getMenuId(); // 让每个请求携带menuid
    // }
    config.headers["appId"] = "admin-web";
    // config.headers['menuId'] =55252102039;
    if (config.method === "post") {
      config.data = JSON.stringify(config.data);
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 返回状态判断
axios.interceptors.response.use(
  (res) => {
    if (res.data.type && res.data.text) {
      if (res.data.type == "danger") {
        Message({
          message: res.data.text,
          type: "error",
          duration: 5 * 1000,
        });
      } else if (res.data.type == "warning") {
        Message({
          message: res.data.text,
          type: "warning",
          duration: 5 * 1000,
        });
      } else if (res.data.type && res.data.text) {
        if (res.data.type == "application/octet-stream" || res.data.type == "application/x-msdownload" || res.data.type == "application/vnd.ms-excel") {
        } else {
          Message({
            message: res.data.text,
            type: "success",
            duration: 5 * 1000,
          });
        }
      }
    }
    if (res.status !== 200 && res.status !== 201) {
      return Promise.reject(res);
    } else {
      return res.data;
    }
  },
  (error) => {
    if (
      error.response &&
      error.response.data &&
      error.response.data.code == 401 &&
      error.response.data.msg == "当前用户未登录"
    ) {
      Message({
        message: "当前用户未登录",
        type: "error",
        duration: 5 * 1000,
      });
      // Vue.$router.push({path: '/login'});
      // window.open("/login");
      // store.dispatch("logOut").then((res) => {
      //   location.reload(); // 为了重新实例化vue-router对象 避免bug
      // });
      return;
    }
    console.log("err", error); // for debug
    if (error.response && error.response.data && error.response.data.type && error.response.data.text) {
      if (error.response.data.type == "danger") {
        Message({
          message: error.response.data.text,
          type: "error",
          duration: 5 * 1000,
        });
      } else if (error.response.data.type == "warning") {
        Message({
          message: error.response.data.text,
          type: "warning",
          duration: 5 * 1000,
        });
      } else {
        Message({
          message: error.response.data.text,
          type: "info",
          duration: 5 * 1000,
        });
      }
    } else {
      Message({
        message: error.message,
        type: "error",
        duration: 5 * 1000,
      });
    }
    return Promise.reject(error);
  }
);

// 获取数据
export default function fetchData(
  url,
  params,
  method = "get",
  contentType,
  responseType
) {
  return new Promise((resolve, reject) => {
    const config = {
      method: method,
      url: SITE + url,
    };
    if (contentType) {
      config.headers = {"Content-Type": contentType};
    }
    if (responseType) {
      config.responseType = responseType;
    }
    method === "get" ? (config.params = params) : (config.data = params);
    axios(config)
      .then(
        (res) => {
          resolve(res);
        },
        (err) => {
          console.log(err);
          reject(err);
        }
      )
      .catch((error) => {
        console.log(error);
        reject(error);
      });
  });
}
